package influence.propagation.implementation.independent.cascade;

import influence.propagation.implementation.RelType;

import java.util.Random;

import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.traversal.Evaluation;
import org.neo4j.graphdb.traversal.Evaluator;

public class IndependentCascadeEvaluator implements Evaluator {

	private Random random = new Random();
	private Label activeL = DynamicLabel.label("ACTIVE");
	
	private double getRandom(){
		return random.nextDouble();
	}
	
	@Override
	public Evaluation evaluate(Path arg0) {
		Node sour = null;
		Node dest = arg0.endNode();
		if (arg0.lastRelationship() != null) {
			sour = arg0.lastRelationship().getStartNode();
		}
		if(arg0.length() == 0){
			return Evaluation.EXCLUDE_AND_CONTINUE;//escludo la prosizione iniziale
		}
		if (dest.hasLabel(activeL)) {
			return Evaluation.EXCLUDE_AND_PRUNE;
		}
		double num = getRandom();
		System.out.println("prob[" + sour.getProperty("NAME") + " to "
				+ dest.getProperty("NAME") + " = " + num + "]");// TODO stringa
																// di debug
		Double prob = (Double) arg0.lastRelationship().getProperty(
				RelType.wheightPropertyKeyName());
		if (num < prob.doubleValue()) {
			dest.addLabel(activeL);
			return Evaluation.INCLUDE_AND_CONTINUE;
		}
		return Evaluation.EXCLUDE_AND_PRUNE;
	}
}
